'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('user_preferences', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '用户ID',
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      preference_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '偏好类型(style, color, occasion, brand等)',
      },
      preference_key: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '偏好键',
      },
      preference_value: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '偏好值',
      },
      preference_data: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '偏好数据(JSON格式)',
      },
      weight: {
        type: Sequelize.INTEGER,
        defaultValue: 1,
        comment: '权重(1-10)',
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '描述',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      deleted_at: {
        type: Sequelize.DATE,
        allowNull: true,
      },
    });

    // 添加索引
    await queryInterface.addIndex('user_preferences', ['user_id']);
    await queryInterface.addIndex('user_preferences', ['preference_type']);
    await queryInterface.addIndex('user_preferences', {
      fields: ['user_id', 'preference_type', 'preference_key'],
      unique: true,
      name: 'unique_user_preference',
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('user_preferences');
  },
};
